package com.itheima.server.controller;

import com.itheima.server.interceptor.UserHolder;
import com.itheima.server.service.UserInfoService;
import com.itheima.tanhua.model.vo.UserInfoVo;
import com.itheima.tanhua.model.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据id查找用户信息
     * @param userId
     * @return
     */
    @GetMapping
    public ResponseEntity findById(Long userId){
        if (userId == null){
            Long tokenUserId = UserHolder.getUserId();
            userId = tokenUserId;
        }
        UserInfoVo userInfoVo = userInfoService.findById(userId);

        return ResponseEntity.ok(userInfoVo);

    }

    @PutMapping
    public ResponseEntity updateById(@RequestBody UserInfo userInfo){
        //先获取用户id
        Long userId = UserHolder.getUserId();
        //将id存入到UserInfo中
        userInfo.setId(userId);

        //再调用Service方法完成修改操作

        userInfoService.update(userInfo);

        return ResponseEntity.ok(null);
    }




}
